package com.yxhl.zoume.utils.alphabet;

import com.yxhl.protobuf.EndStationEntry;
import com.yxhl.protobuf.ScheduleType;
import com.yxhl.protobuf.StartCityEntry;
import com.yxhl.zoume.core.busticket.model.BusSelectCityEntity;
import com.yxhl.zoume.core.busticket.model.ObservableLetterEntity;
import com.yxhl.zoume.core.busticket.ui.info.ZMScheduleType;
import com.yxhl.zoume.core.car.model.CarSelectPassengerEntity;
import com.yxhl.zoume.core.car.model.SelectCityEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by sugarya on 16/7/26.
 */
public class AlphabetHelper {

    public static final String TAG = "AlphabetHelper";

    private static final String[] mLetters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    /**
     * 生成巴士模块目的地页面的观察内容
     *
     * @param dataList
     * @return
     */
    public static Observable<ObservableLetterEntity> getArrivalAlphabetObservable(List<EndStationEntry> dataList) {
        final ObservableLetterEntity observableLetterEntity = new ObservableLetterEntity();
        Observable<ObservableLetterEntity> letterEntityObservable = Observable
                .from(dataList)
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.newThread())
                .flatMap(new Func1<EndStationEntry, Observable<ObservableLetterEntity>>() {
                    @Override
                    public Observable<ObservableLetterEntity> call(EndStationEntry endStationEntry) {
                        String cityName = endStationEntry.getEndStationName();
                        String cityCode = endStationEntry.getEndStationCode();
                        String letter = Pinyin4jUtils.getInitialLetter2(cityName);
                        List<ZMScheduleType> zmScheduleTypeList = getZMScheduleTypeList(endStationEntry.getTypeList());

                        observableLetterEntity.setLetter(letter);
                        observableLetterEntity.setCityName(cityName);
                        observableLetterEntity.setCityCode(cityCode);
                        observableLetterEntity.setScheduleTypeList(zmScheduleTypeList);

                        return Observable.just(observableLetterEntity);
                    }
                });

        return letterEntityObservable;

    }

    private static List<ZMScheduleType> getZMScheduleTypeList(List<ScheduleType> scheduleTypeList) {
        List<ZMScheduleType> zmScheduleTypeArrayList = new ArrayList<>();

        for (ScheduleType scheduleType : scheduleTypeList) {
            ZMScheduleType zmScheduleType;
            switch (scheduleType) {
                case V_DIY_BUS:
                    zmScheduleType = ZMScheduleType.V_DIY_BUS;
                    break;
                case V_FT_BUS:
                    zmScheduleType = ZMScheduleType.V_FT_BUS;
                    break;
                case V_XD_BUS:
                    zmScheduleType = ZMScheduleType.V_XD_BUS;
                    break;
                case V_DABA_BUS:
                    zmScheduleType = ZMScheduleType.V_DABA_BUS;
                    break;
                default:
                    zmScheduleType = null;
            }
            zmScheduleTypeArrayList.add(zmScheduleType);
        }

        return zmScheduleTypeArrayList;
    }

    /**
     * 生成巴士模块出发城市页面的观察内容
     *
     * @param dataList
     * @return
     */
    public static Observable<ObservableLetterEntity> getStartAlphabetObservable(List<StartCityEntry> dataList) {
        final ObservableLetterEntity observableLetterEntity = new ObservableLetterEntity();
        Observable<ObservableLetterEntity> letterEntityObservable = Observable
                .from(dataList)
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.newThread())
                .flatMap(new Func1<StartCityEntry, Observable<ObservableLetterEntity>>() {
                    @Override
                    public Observable<ObservableLetterEntity> call(StartCityEntry startCityEntry) {
                        String cityName = startCityEntry.getCityName();
                        String cityCode = startCityEntry.getCityCode();
                        ArrayList<ScheduleType> scheduleTypeList = new ArrayList<>();
                        scheduleTypeList.add(startCityEntry.getType());
                        String letter = Pinyin4jUtils.getInitialLetter2(cityName);

                        observableLetterEntity.setLetter(letter);
                        observableLetterEntity.setCityName(cityName);
                        observableLetterEntity.setCityCode(cityCode);
                        observableLetterEntity.setScheduleTypeList(getZMScheduleTypeList(scheduleTypeList));

                        return Observable.just(observableLetterEntity);
                    }
                });
        return letterEntityObservable;
    }

    /**
     * 获取巴士出发/到达城市 字母表联动的展示数据
     *
     * @param dataMap
     * @return
     */
    public static List<BusSelectCityEntity> getBusAlphabetData(Map<String, List<BusSelectCityEntity>> dataMap) {
        List<BusSelectCityEntity> ret = new ArrayList<>();
        for (String letter : mLetters) {
            if (dataMap.containsKey(letter)) {
                List<BusSelectCityEntity> busSelectCityEntityList = dataMap.get(letter);
                if (busSelectCityEntityList != null && busSelectCityEntityList.size() > 0) {
                    ret.addAll(busSelectCityEntityList);
                }
            }
        }
        return ret;
    }

    /**
     * 获取手机联系人 字母表展示所需的数据
     *
     * @param dataMap
     * @return
     */
    public static List<CarSelectPassengerEntity> getCarAlphabetData(Map<String, List<CarSelectPassengerEntity>> dataMap) {
        List<CarSelectPassengerEntity> ret = new ArrayList<>();
        for (String letter : mLetters) {
            if (dataMap.containsKey(letter)) {
                List<CarSelectPassengerEntity> passengerEntityList = dataMap.get(letter);
                if (passengerEntityList != null && passengerEntityList.size() > 0) {
                    ret.addAll(passengerEntityList);
                }
            }
        }
        return ret;
    }

    /**
     * 手机联系人数据处理
     *
     * @param dataMap
     * @param carSelectPassengerEntity
     */
    public static void mobileContactHandler(HashMap<String, List<CarSelectPassengerEntity>> dataMap, CarSelectPassengerEntity carSelectPassengerEntity) {

        String displayContent = carSelectPassengerEntity.getDisplayContent();
        String letter = getInitialLetter(displayContent);

        List<CarSelectPassengerEntity> passengerEntityList = dataMap.get(letter);
        if (passengerEntityList != null) {
            passengerEntityList.add(carSelectPassengerEntity);
        } else {
            passengerEntityList = new ArrayList<>();
            passengerEntityList.add(new CarSelectPassengerEntity(letter, ""));
            passengerEntityList.add(carSelectPassengerEntity);
            dataMap.put(letter, passengerEntityList);
        }
    }


    /**
     * 城市数据处理
     *
     * @param dataMap
     * @param
     */
    public static void processCityList(HashMap<String, List<SelectCityEntity>> dataMap, SelectCityEntity cityEntity) {

        String displayContent = cityEntity.getDisplayContent();
        String letter = getInitialLetter(displayContent);

        List<SelectCityEntity> cityEntityList = dataMap.get(letter);
        if (cityEntityList != null) {
            cityEntityList.add(cityEntity);
        } else {
            cityEntityList = new ArrayList<>();
            cityEntityList.add(new SelectCityEntity(letter));
            cityEntityList.add(cityEntity);
            dataMap.put(letter, cityEntityList);
        }
    }

    /**
     * 获取手机联系人 字母表展示所需的数据
     *
     * @param dataMap
     * @return
     */
    public static List<SelectCityEntity> getCityAlphabetData(Map<String, List<SelectCityEntity>> dataMap) {
        List<SelectCityEntity> ret = new ArrayList<>();
        for (String letter : mLetters) {
            if (dataMap.containsKey(letter)) {
                List<SelectCityEntity> cityEntityList = dataMap.get(letter);
                if (cityEntityList != null && cityEntityList.size() > 0) {
                    ret.addAll(cityEntityList);
                }
            }
        }
        return ret;
    }


//    /**
//     * 处理并生成字母表联动数据集
//     *
//     * @param dataMap key 字母,value对应城市名
//     * @return
//     */
//    public static List<BusSelectCityEntity> alphabetDataHandler(Map<String,List<BusSelectCityEntity>> dataMap,
//                                                         List<BusSelectCityEntity> adapterDataList,
//                                                         Handler handler
//    ) {
//        //反复后退,新建页面,有可能adapter的数据集不为空(avoid consistent detected error)
//        int initialSize = adapterDataList.size();
//        if (initialSize > 0) {
//            adapterDataList.clear();
//            Message message = handler.obtainMessage();
//            message.what = BusArrivalStationFragment.MSG_WHAT_NOTIFY_RECYCLER_PRE;
//            message.arg1 = 0;
//            message.arg2 = initialSize;
//            handler.sendMessage(message);
//        }
//
//        List<BusSelectCityEntity> ret = new ArrayList<>();
//        int alreadyInsertedSize = 0;
//        boolean enable = true;
//        for (String letter : mLetters) {
//            if (dataMap.containsKey(letter)) {
//                List<BusSelectCityEntity> oneLetterEntityList = dataMap.get(letter);
//                if (oneLetterEntityList != null && oneLetterEntityList.size() > 0) {
//                    ret.addAll(oneLetterEntityList);
//                    adapterDataList.addAll(oneLetterEntityList);
//
//                    switch (letter) {
//                        case "D":
//                        case "E":
//                        case "F":
//                            if (enable) {
//                                LOG.e(TAG, "letter = " + letter);
//                                alreadyInsertedSize = beginningInsertHandler(adapterDataList, handler);
//                                enable = false;
//                            }
//                            try {
//                                Thread.sleep(300);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                            break;
//                        case "L":
//                        case "S":
//                            LOG.e(TAG, "letter = " + letter);
//                            alreadyInsertedSize = middleWayInsertHandler(adapterDataList, handler, alreadyInsertedSize);
//                            try {
//                                Thread.sleep(300);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                            break;
//                        case "Z":
//                            LOG.e(TAG, "letter = " + letter);
//                            endingInsertHandler(adapterDataList, handler, alreadyInsertedSize);
//                            break;
//                        default:
//
//                    }
//                }
//            }
//        }
//
//        if (!dataMap.containsKey("Z")) {
//            endingInsertHandler(adapterDataList, handler, alreadyInsertedSize);
//        }
//
//        return ret;
//    }
//
//    /**
//     * 字母表最开始的插入处理
//     *
//     * @param adapterDataList Adapter数据集
//     * @param handler
//     * @return
//     */
//    private static int beginningInsertHandler(List<BusSelectCityEntity> adapterDataList, Handler handler) {
//        int totalSize = adapterDataList.size();
//
//        Message message = handler.obtainMessage();
//        message.what = BusArrivalStationFragment.MSG_WHAT_NOTIFY_RECYCLER_JUNIOR_ANDROID_VERSION;
//        message.arg1 = 0;
//        message.arg2 = totalSize;
//        Log.e(TAG,"beginningInsertHandler arg1=0"+"   arg2="+totalSize);
//        handler.sendMessage(message);
//
//        return totalSize;
//    }
//
//    /**
//     * 字母表中途插入处理
//     *
//     * @param adapterDataList
//     * @param handler
//     * @param alreadyInsertedSize
//     */
//    private static int middleWayInsertHandler(List<BusSelectCityEntity> adapterDataList, Handler handler, int alreadyInsertedSize) {
//
//        int arg1;
//        int arg2;
//
//        int currentTotalSize = adapterDataList.size();
//        if (alreadyInsertedSize <= 0) {
//            arg1 = 0;
//            arg2 = currentTotalSize;
//        } else {
//            arg1 = alreadyInsertedSize - 1;
//            arg2 = currentTotalSize - alreadyInsertedSize;
//        }
//
//        LOG.e(TAG,"middleWayInsertHandler arg1="+arg1 + "  arg2="+arg2);
//        Message msg = handler.obtainMessage();
//        msg.what = BusArrivalStationFragment.MSG_WHAT_NOTIFY_RECYCLER_SENIOR_ANDROID_VERSION;
//        msg.arg1 = arg1;
//        msg.arg2 = arg2;
//        handler.sendMessage(msg);
//
//        return currentTotalSize;
//    }
//
//    /**
//     * 字母表结尾插入处理
//     * @param adapterDataList
//     * @param handler
//     * @param alreadyInsertedSize
//     */
//    private static void endingInsertHandler(List<BusSelectCityEntity> adapterDataList, Handler handler, int alreadyInsertedSize) {
//
//        int arg1;
//        int arg2;
//
//        int finalTotalSize = adapterDataList.size();
//        if (alreadyInsertedSize <= 0) {
//            arg1 = 0;
//            arg2 = finalTotalSize;
//        } else if (alreadyInsertedSize < finalTotalSize) {
//            arg1 = alreadyInsertedSize - 1;
//            arg2 = finalTotalSize - alreadyInsertedSize;
//        } else {
//            return;
//        }
//
//        LOG.e(TAG,"endingInsertHandler arg1="+arg1 + "  arg2="+arg2);
//        Message msg = handler.obtainMessage();
//        msg.what = BusArrivalStationFragment.MSG_WHAT_NOTIFY_RECYCLER_ENDING;
//        msg.arg1 = arg1;
//        msg.arg2 = arg2;
//        handler.sendMessage(msg);
//    }


    public static String getInitialLetter(String content) {
        return Pinyin4jUtils.getInitialLetter2(content);
    }

    /**
     * 简拼
     *
     * @param str
     * @return
     */
    public static String converterToShortSpell(String str) {
        return Pinyin4jUtils.converterToShortSpell(str);
    }

    /**
     * 全拼
     *
     * @param str
     * @return
     */
    public static String converterToFullSpell(String str) {
        return Pinyin4jUtils.converterToFullSpell(str);
    }

}
